﻿/*
 *  This file is part of SXAPI.
 *
 *  SXAPI is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, version 3 of the License.

 *  SXAPI is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with SXAPI.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace StackExchangeApi
{
    /// <summary>
    /// An companion class to Question that eagerly fetches
    /// all of its properties.
    /// 
    /// To use, obtain an Question instance and cast it (implicitly
    /// or otherwise) to an QuestionAnswer.
    /// </summary>
    public sealed class EagerQuestion : Post
    {
        /// <summary>
        /// Implicitly cast a Question to an EagerQuestion.
        /// 
        /// Doing so causes all the Properties on the Question
        /// to be fetched.
        /// </summary>
        /// <param name="question">The Question to eagerly load.</param>
        /// <returns>An EagerQuestion with all its properties fetched.</returns>
        public static implicit operator EagerQuestion(Question question)
        {
            return new EagerQuestion(question);
        }

        /// <summary>
        /// Number of users who have favorited this question
        /// </summary>
        public long FavoriteCount
        {
            get;
            internal set;
        }

        /// <summary>
        /// Number of answers to this question
        /// </summary>
        public long AnswerCount
        {
            get;
            internal set;
        }

        /// <summary>
        /// Number of views on this question
        /// </summary>
        public long ViewCount
        {
            get;
            internal set;
        }

        /// <summary>
        /// Tags on this question
        /// </summary>
        public ReadOnlyCollection<String> Tags
        {
            get;
            internal set;
        }

        /// <summary>
        /// Date this question was created
        /// </summary>
        public DateTime Created
        {
            get;
            internal set;
        }

        /// <summary>
        /// Date this question was last editted
        /// </summary>
        public DateTime LastEdit
        {
            get;
            internal set;
        }

        /// <summary>
        /// The answers for this question
        /// </summary>
        public ReadOnlyCollection<Answer> Answers
        {
            get;
            internal set;
        }

        /// <summary>
        /// Construct a new EagerQuestion.
        /// 
        /// Uses reflection to iterate over all properties on
        /// ans and set its own properties accordingly.
        /// 
        /// This guarantees that all would-be-delayed loading
        /// is infact run immediately.
        /// </summary>
        /// <param name="ques">The Question in question</param>
        internal EagerQuestion(Question ques)
            : base(ques.Id, ques.Site)
        {
            Utils.EagerlyLoad(ques, this, typeof(Question), typeof(EagerQuestion));
        }
    }
}
